#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce :
@File      : celery.py
@Time      : 2023/9/28 16:38
@Author    : toby

:des  :参考   https://zhuanlan.zhihu.com/p/162039762
https://www.jianshu.com/p/f488f1deed61
"""


import os
from celery import Celery
from datetime import timedelta
from django.conf import settings
import time


# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django32_celery.settings')
app = Celery('django32_celery',
                            broker ="redis://localhost:6379/0",
                            backend ='redis://mast:{}@106.52.179.127:26002/3'.format('261_Xlr_e8ec5febde5edadc7fdb0fc4d3'))
# Using a string here means the worker doesn't have to serialize
#   should have a `CELERY_` prefix.  这里指定从django的settings.py里读取celery配置
app.config_from_object('django.conf:settings', namespace='CELERY')


from c_apps.tasks import send_sms
# 定时任务调度   可以自行成功
app.conf.beat_schedule = {
    "devices_autostart": {    # 取个名字
        'task': 'c_apps.tasks.send_sms',   # 设置是要将哪个任务进行定时
        # 'task': 'api_test',   # 设置是要将哪个任务进行定时
        "schedule": timedelta(minutes=5),
        'args': ()   # 传递参数
    }
}

app.conf.beat_schedule = {
    "interface_test": {    # 取个名字
        'task': 'c_apps.tasks.crontab_job',   # 设置是要将哪个任务进行定时
        "schedule": timedelta(minutes=120),
        'args': ()   # 传递参数
    }
}





@app.task(bind=True)
def debug_task(self):
    time.sleep(1)
    print("current task is beat !")
    print(f'Request: {self.request!r}')


# Load task modules from all registered Django apps.  # 自动从所有已注册的django app中加载任务
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)